﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Xinyi.Common;
using Xinyi.Data;

namespace web1.admin
{
    public partial class pro_add : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            FunctionClass.CheckAdminLogin(0);
            if (!IsPostBack)
            {
                BindData();
            }
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        private void BindData()
        {
            string strSql;

            strSql = "select * from T_ProType order by PID,SID";

            ddlType.Items.Add(new ListItem("请选择", "0"));

            DataClass myData = new DataClass();
            SqlConnection myConn = myData.ConnOpen();

            DataSet myDs = myData.GetDataSet(strSql, myConn);
            myData.InitProTypeList(myDs.Tables[0], 0, ddlType, "");
            myDs.Dispose();

            myData.ConnClose(myConn);

            tbPicWidth.Attributes.Add("onkeyup", "value=value.replace(/[^\\d]/g,'')");
            tbPicHeight.Attributes.Add("onkeyup", "value=value.replace(/[^\\d]/g,'')");
            tbPicWidth1.Attributes.Add("onkeyup", "value=value.replace(/[^\\d]/g,'')");
            tbPicHeight1.Attributes.Add("onkeyup", "value=value.replace(/[^\\d]/g,'')");
            tbPicWidth2.Attributes.Add("onkeyup", "value=value.replace(/[^\\d]/g,'')");
            tbPicHeight2.Attributes.Add("onkeyup", "value=value.replace(/[^\\d]/g,'')");
            tbPicWidth3.Attributes.Add("onkeyup", "value=value.replace(/[^\\d]/g,'')");
            tbPicHeight3.Attributes.Add("onkeyup", "value=value.replace(/[^\\d]/g,'')");

            tbPrice.Attributes.Add("onkeyup", "value=value.replace(/[^\\d]/g,'')");
            tbPrice1.Attributes.Add("onkeyup", "value=value.replace(/[^\\d]/g,'')");
        }

        protected void btSubmit_Click(object sender, EventArgs e)
        {
            string strSql, strTypeID, strProName, strProModel, strProTip, strProPic, strIsShow, strIsRecommend, strContent, strKeyWords;
            string strSeo_Title, strSeo_Keywords, strSeo_Description, strSeo_Author, strLinks, strIsNew, strPrice, strPrice1;
            string strProPic1, strProPic2, strProPic3;
            string strProGrade, strProAreas, strProWeight, strProPackage, strProStorage, strShelfLife;

            strTypeID = ddlType.SelectedValue;
            strProName = tbProName.Text;
            strProModel = tbProModel.Text;
            strProTip = tbProTip.Text;
            strIsShow = cblIsShow.SelectedValue;
            strIsRecommend = cblIsRecommend.SelectedValue;
            strContent = tbContent.Text;
            strKeyWords = tbKeyWords.Text;
            strLinks = tbLinks.Text;
            strIsNew = cblIsNew.SelectedValue;
            strPrice = tbPrice.Text;
            strPrice1 = tbPrice1.Text;
            strProGrade = tbProGrade.Text;
            strProAreas = tbProAreas.Text;
            strProWeight = tbProWeight.Text;
            strProPackage = tbProPackage.Text;
            strProStorage = tbProStorage.Text;
            strShelfLife = tbShelfLife.Text;

            //SEO选项
            strSeo_Title = tbSeo_Title.Text;
            strSeo_Keywords = tbSeo_Keywords.Text;
            strSeo_Description = tbSeo_Description.Text;
            strSeo_Author = tbSeo_Author.Text;

            //判断title是否为空
            if (strSeo_Title == "")
                strSeo_Title = strProName;

            FunctionClass myFun=new FunctionClass();

            strProPic = myFun.UploadFile(fuProPic, "../upload/", "jpg|png|gif", 512 * 10);
            strProPic1 = myFun.UploadFile(fuProPic1, "../upload/", "jpg|png|gif", 512 * 10);
            strProPic2 = myFun.UploadFile(fuProPic2, "../upload/", "jpg|png|gif", 512 * 10);
            strProPic3 = myFun.UploadFile(fuProPic3, "../upload/", "jpg|png|gif", 512 * 10);

            //缩略图
            if (strProPic != "")//判断是否有上传图片
            {
                Thumbnail myTb = new Thumbnail(strProPic, tbPicHeight.Text, tbPicWidth.Text);
                //判断是否出错
                if (myTb.ErrorMessage != null)
                {
                    FunctionClass.ShowMsgBox(myTb.ErrorMessage);
                    Response.End();
                }
            }
            //缩略图1
            if (strProPic1 != "")//判断是否有上传图片
            {
                Thumbnail myTb = new Thumbnail(strProPic1, tbPicHeight1.Text, tbPicWidth1.Text);
                //判断是否出错
                if (myTb.ErrorMessage != null)
                {
                    FunctionClass.ShowMsgBox(myTb.ErrorMessage);
                    Response.End();
                }
            }
            //缩略图2
            if (strProPic2 != "")//判断是否有上传图片
            {
                Thumbnail myTb = new Thumbnail(strProPic2, tbPicHeight2.Text, tbPicWidth2.Text);
                //判断是否出错
                if (myTb.ErrorMessage != null)
                {
                    FunctionClass.ShowMsgBox(myTb.ErrorMessage);
                    Response.End();
                }
            }
            //缩略图3
            if (strProPic3 != "")//判断是否有上传图片
            {
                Thumbnail myTb = new Thumbnail(strProPic3, tbPicHeight3.Text, tbPicWidth3.Text);
                //判断是否出错
                if (myTb.ErrorMessage != null)
                {
                    FunctionClass.ShowMsgBox(myTb.ErrorMessage);
                    Response.End();
                }
            }

            strSql = "insert into T_Products (TypeID,ProName,ProModel,ProTip,ProPic,IsShow,IsRecommend,[Content],KeyWords,Seo_Title,Seo_Keywords,Seo_Description,"
                + "Seo_Author,Links,IsNew,Price,Price1,ProPic1,ProPic2,ProPic3,ProGrade,ProAreas,ProWeight,ProPackage,ProStorage,ShelfLife) values"
                +" (@TypeID,@ProName,@ProModel,@ProTip,@ProPic,@IsShow,@IsRecommend,@Content,@KeyWords,@Seo_Title,@Seo_Keywords,@Seo_Description,"
                + "@Seo_Author,@Links,@IsNew,@Price,@Price1,@ProPic1,@ProPic2,@ProPic3,@ProGrade,@ProAreas,@ProWeight,@ProPackage,@ProStorage,@ShelfLife)";
            string[] ParamsName = new string[] { "@TypeID","@ProName","@ProModel","@ProTip","@ProPic","@IsShow","@IsRecommend","@Content","@KeyWords", 
                "@Seo_Title", "@Seo_Keywords", "@Seo_Description", "@Seo_Author","@Links","@IsNew","@Price","@Price1","@ProPic1","@ProPic2","@ProPic3",
                "@ProGrade","@ProAreas","@ProWeight","@ProPackage","@ProStorage","@ShelfLife"};
            string[] ParamsValue = new string[] { strTypeID, strProName,strProModel,strProTip,strProPic,strIsShow,strIsRecommend,strContent,strKeyWords,
                strSeo_Title,strSeo_Keywords,strSeo_Description,strSeo_Author,strLinks ,strIsNew,strPrice,strPrice1,strProPic1,strProPic2,strProPic3,
                strProGrade,strProAreas,strProWeight,strProPackage,strProStorage,strShelfLife};

            DataClass myData = new DataClass();
            SqlConnection myConn = myData.ConnOpen();

            myData.InsertData(strSql, ParamsName, ParamsValue, myConn);

            myData.ConnClose(myConn);

            FunctionClass.ShowMsgBox("添加成功！","");
            Response.End();
        }
    }
}
